import {
  getNodeSearchLogList,
  getNodeUploadLogList,
  getNodeUploadDetail,
} from "@/api/supply";
import { taskStatusEnum } from "./server";

export default {
  data() {
    return {
      searchVisible: false,
      uploadVisible: false,
      currRow: {},
      recordType: "",
      recordList: [],
      recordTotal: 0,
      recordLoading: false,
      recordParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
      },
      jsonData: "",
      jsonVisible: false,
      taskStatusEnum,
      supplyVisible: false,
      resourceSupply: null,
      elementProvide: null,
      elementVisible: false,
    };
  },
  watch: {
    searchVisible(val) {
      if (val) this.getRecordDetail();
    },
    uploadVisible(val) {
      if (val) this.getRecordDetail();
    },
  },
  methods: {
    checkJsonDetail(json) {
      this.jsonData = json
        .replaceAll("//", "")
        .replaceAll("[", "[<br/>&nbsp;&nbsp;")
        .replaceAll("{", "{<br/>&nbsp;&nbsp;&nbsp;&nbsp;")
        .replaceAll(",", ",<br/>&nbsp;&nbsp;&nbsp;&nbsp;")
        .replaceAll("}", "<br/>&nbsp;&nbsp;}")
        .replaceAll("]", "<br/>]");
      this.jsonVisible = true;
    },
    openRecord(row, type) {
      this.currRow = row;
      this.recordType = type;
      this.$nextTick(() => {
        if (type === "search") {
          this.searchVisible = true;
        } else {
          this.uploadVisible = true;
        }
      });
    },
    async getRecordDetail() {
      const { id: nodeId, nodeLoginCode } = this.currRow;
      const api =
        this.recordType === "search"
          ? getNodeSearchLogList
          : getNodeUploadLogList;
      this.recordLoading = true;
      try {
        const { data, total } = await api({
          ...this.recordParams,
          nodeId,
          nodeLoginCode,
        });
        this.recordList = data;
        this.recordTotal = total;
      } finally {
        this.recordLoading = false;
      }
    },
    reGetRecord() {
      this.$set(this.recordParams, "pageNum", 1);
      this.getRecordDetail();
    },
    closeRecordDialog() {
      this.recordType = "";
      this.searchVisible = false;
      this.uploadVisible = false;
    },
    async doUploadSync(id) {
      const { data } = await getNodeUploadDetail({ uploadId: id });
      if (!data) return;
      if (data.editType === 3) {
        this.resourceSupply = data.resourceSupply;
        this.supplyVisible = true;
      } else if (data.editType === 5) {
        this.elementProvide = data.elementProvide;
        this.elementVisible = true;
      }
    },
  },
};
